<?php
Class Controller_Manage_Base extends Controller_Abstract
{
	function actionNodeCreate()
	{
		$this->_view['title'] = '添加节点类型';
	}
	
	function actionNodeCreateSave()
	{
		$this->_view['title'] = '添加节点类型保存';
		$note = isset($_POST['note'])?$_POST['note']:'';
		$number = isset($_POST['number'])?$_POST['number']:'';
		$name = isset($_POST['name'])?$_POST['name']:'';
		
		if($name && $number)
		{
			$new = new NoteType();
			$new->name = $name;
			$new->number = $number;
			$new->note = $note;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Base/NodeCreate'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "编号和名称不能为空", url('Manage::Base/NodeCreate'),FAILURE);
		}
	}
	
	function actionNoteTypeList()
	{
		$this->_view['title'] = '节点类型列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = NoteType::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Base/NoteTypeList', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['notetype'] = $result['data'];
		}
	}

    function actionShow()
    {
        $this->_view['title'] = '显示素材详情';
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        $code = Information::find('id=?', $id)->asArray()->getOne();
        if ($code)
        {
            $this->_view['code'] =  $code['code'];
        }
        else
        {
            return $this->_redirectMessage('查询失败', '提交参数错误', url('Manage::Information/List'), FAILURE);
        }
    }
	
	function actionDetail()
	{
		$this->_view['title'] = '节点类型详细';
	}
	
	function actionNoteTypeEdit()
	{
		$this->_view['title'] = '节点类型编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$node = NoteType::find('id=?', $id)->asArray()->getOne();
		if ($node)
		{
			$this->_view['node'] = $node;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '没有该产品存在', url('Manage::Base/NoteTypeList'), FAILURE);
		}
	}
	
	function actionNoteTypeEditSave()
	{
		$this->_view['title'] = '节点类型编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$note = isset($_POST['note'])?$_POST['note']:'';
			$number = isset($_POST['number'])?$_POST['number']:'';
			$name = isset($_POST['name'])?$_POST['name']:'';
			
			if($name && $number && $id)
			{
				$edit = NoteType::find('id=?', $id)->getOne();
				
				$edit->name = $name;
				$edit->number = $number;
				$edit->note = $note;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Base/NoteTypeList'), FAILURE);
		}
	}
	
	function actionNoteTypeDelete()
	{
		$this->_view['title'] = '节点删除';
		$result = NoteType::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionNodeDeleteAll()
	{
		$this->_view['title'] = '勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = NoteType::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionUnitList()
	{
		$this->_view['title'] = '指标单位列表';
		$cp = isset($_GET['cp']) ? $_GET['cp'] : 1;
		$ps = isset($_GET['ps']) ? $_GET['ps'] : 20;
		$url_arr = Helper_BSS_Normal::buildCondition();
		$result = VegeIndexUnit::search($cp, $ps, $url_arr);
		if ($result['ack'] == SUCCESS)
		{
			$page = new Helper_BSS_Pagec($result['pagination'], url('Manage::Base/UnitList', $url_arr));
			$this->_view['page'] = $page->getPage();
            $this->_view['pagecount'] = $result['pagination'];
			$this->_view['list'] = $result['data'];
		}
	}
	
	function actionUnitCreate()
	{
		$this->_view['title'] = '添加指标单位类型';
	}
	
	function actionUnitCreateSave()
	{
		$this->_view['title'] = '添加指标单位保存';
		$note = isset($_POST['note'])?$_POST['note']:'';
		$type = isset($_POST['type'])?$_POST['type']:'';
		$name = isset($_POST['name'])?$_POST['name']:'';
		
		if($name && $type)
		{
			$new = new VegeIndexUnit();
			$new->name = $name;
			$new->type = $type;
			$new->note = $note;
			$new->save();//终极方法
			return $this->_redirectMessage('提交成功', "正在返回..", url('Manage::Base/UnitCreate'),SUCCESS);
		}
		else
		{
			return $this->_redirectMessage('提交失败', "编号和名称不能为空", url('Manage::Base/UnitCreate'),FAILURE);
		}
	}
	
	function actionUnitEdit()
	{
		$this->_view['title'] = '指标单位编辑';
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$unit = VegeIndexUnit::find('id=?', $id)->asArray()->getOne();
		if ($unit)
		{
			$this->_view['unit'] = $unit;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '没有该产品存在', url('Manage::Base/UnitList'), FAILURE);
		}
	}
	
	function actionUnitEditSave()
	{
		$this->_view['title'] = '指标单位编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$note = isset($_POST['note'])?$_POST['note']:'';
			$type = isset($_POST['type'])?$_POST['type']:'';
			$name = isset($_POST['name'])?$_POST['name']:'';
			
			if($name && $type && $id)
			{
				$edit = VegeIndexUnit::find('id=?', $id)->getOne();
				
				$edit->name = $name;
				$edit->type = $type;
				$edit->note = $note;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Base/UnitList'), FAILURE);
		}
	}
	
	function actionUnitDelete()
	{
		$this->_view['title'] = '指标单位删除';
		$result = VegeIndexUnit::meta()->deleteWhere(array('id'=>$_GET['id']));
		if (!$result)
		{
            echo 1;exit;
		}
		else
		{
            echo 0;exit;
		}
	}
	
	function actionUnitDeleteAll()
	{
		$this->_view['title'] = '指标单位勾选删除';
		if ($this->_context->isGET())
		{
			$ids = explode(',', $_GET['ids']);
			$msg = '';
			foreach ($ids as $i)
			{
				$result = VegeIndexUnit::meta()->deleteWhere(array('id'=>$i));
				if (!$result)
				{
					$msg = '';
				}
				else
				{
					$msg = $i.'删除失败';
				}
			}

			if (!$msg)
			{
				return $this->_redirectMessage('批量删除成功', '正在返回..', $_SERVER['HTTP_REFERER'], SUCCESS);
			}
			else
			{
				return $this->_redirectMessage('删除失败', '请检查提交的数据是否存在', $_SERVER['HTTP_REFERER'], FAILURE);
			}
		}
	}
	
	function actionBaseNodeInfoEdit()
	{
		$this->_view['title'] = '主体信息编辑';
		$vegebusinesstype = VegeBusinessType::find()->asArray()->getAll();
		$this->_view['vegebusinesstype'] = $vegebusinesstype;
		$notetype = NoteType::find()->asArray()->getAll();
		$this->_view['notetype'] = $notetype;
		
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$basenodeinfo = BaseNodeInfo::find('id=?', $id)->asArray()->getOne();
		if ($basenodeinfo)
		{
			$this->_view['basenodeinfo'] = $basenodeinfo;
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '不存在该主体信息', url('Manage::Base/BaseNodeInfoEdit'), FAILURE);
		}
	}
	
	function actionBaseNodeInfoEditSave()
	{
		$this->_view['title'] = '主体信息编辑保存';
		if (isset($_POST)&&$_POST)
		{
			$id = isset($_POST['id'])?$_POST['id']:'';
			$companyname = isset($_POST['companyname'])?$_POST['companyname']:'';
			$companynumber = isset($_POST['companynumber'])?$_POST['companynumber']:'';
			$regnumber = isset($_POST['regnumber'])?$_POST['regnumber']:'';
			$nodetype = isset($_POST['nodetype'])?$_POST['nodetype']:'';
			$area = isset($_POST['area'])?$_POST['area']:'';
			$recorddate = isset($_POST['recorddate'])?$_POST['recorddate']:'';
			$corporate = isset($_POST['corporate'])?$_POST['corporate']:'';
			$address = isset($_POST['address'])?$_POST['address']:'';
			$phone = isset($_POST['phone'])?$_POST['phone']:'';
			$fax = isset($_POST['fax'])?$_POST['fax']:'';
			$time = date('Y-m-d H:i:s');
			
			if(strlen($phone)<11)
			{
				return $this->_redirectMessage('提交失败', "请填写正确的手机号码", url('Manage::Base/BaseNodeInfoEdit'),FAILURE);
			}
			
			if($companyname && $companynumber && $id && $phone)
			{
				$edit = BaseNodeInfo::find('id=?', $id)->getOne();
				$edit->companyname = $companyname;
				$edit->companynumber = $companynumber;
				$edit->regnumber = $regnumber;
				$edit->nodetype = $nodetype;
				$edit->area = $area;
				$edit->recorddate = $recorddate;
				$edit->corporate = $corporate;
				$edit->address = $address;
				$edit->phone = $phone;
				$edit->fax = $fax;
				$edit->time = $time;
				$edit->save();//终极方法
				
				return $this->_redirectMessage('提交成功', "正在返回..", $_SERVER['HTTP_REFERER'],SUCCESS);//只返回上一页即可，这样好定位
			}
			else
			{
				return $this->_redirectMessage('提交失败', "编号和名称不能为空", $_SERVER['HTTP_REFERER'],FAILURE);
			}
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '请检查提交的数据是否正确', url('Manage::Base/BaseNodeInfoEdit'), FAILURE);
		}
	}
	
	function actionExportBaseNodeInfo()
	{
		$this->_view['title'] = '导出主体信息';
		set_time_limit(0);
		$id = isset($_GET['id']) ? $_GET['id'] : 0;
		$basenodeinfo = BaseNodeInfo::find('id=?', $id)->asArray()->getOne();
		if ($basenodeinfo)
		{
			$filename = $basenodeinfo['companynumber'] . '_QYDA.txt'; //设置文件名
			header('Content-Type: text/txt');
			header("Content-Disposition: attachment;filename={$filename}");
			$out = fopen('php://output', 'w');
			$line = ",".$basenodeinfo['companynumber'];
			$line .= ",".$basenodeinfo['companyname'];
			$line .= ",".$basenodeinfo['regnumber'];
			$line .= ",".$basenodeinfo['nodetype'];
			$line .= ",".$basenodeinfo['area'];
			$line .= ",".str_replace('-','',$basenodeinfo['recorddate']);
			$line .= ",".$basenodeinfo['corporate'];
			$line .= ",".$basenodeinfo['address'];
			$line .= ",".$basenodeinfo['phone'];
			$line .= ",".$basenodeinfo['fax'];
			fputs($out, $line);
			//fputcsv($out, ['bianhao']);
			//fputs($out, '防伪码'."\r\n");
			/*foreach ($rows as $row) 
			{
				$line = ",".$row['bianhao']."\r\n";    
				//fputcsv($out, $line);
				fputs($out, $line);
			}*/
			fclose($out);
		}
		else
		{
			return $this->_redirectMessage('编辑失败', '不存在该主体信息', url('Manage::Base/BaseNodeInfoEdit'), FAILURE);
		}
	}
	
	function actionChangePass()
	{
		$this->_view['title'] = '密码修改';
	}
	
	function actionChangePassSave()
	{
		$this->_view['title'] = '密码修改保存';
		if ($this->_context->isPOST())
		{
			$user = $this->_app->currentUser();
			if (empty($_POST['old_password']))
			{
				return $this->_redirectMessage('旧密码为空！', '请输入您的旧密码！', url('Manage::Base/ChangePass'), FAILURE);
			}
			
			if (empty($_POST['new_password']))
			{
				return $this->_redirectMessage('新密码为空！', '请输入您的新密码！', url('Manage::Base/ChangePass'), FAILURE);
			}
			
			if (empty($_POST['new_password_again']))
			{
				return $this->_redirectMessage('确认密码为空！', '请确认密码！', url('Manage::Base/ChangePass'), FAILURE);
			}
			
			if (trim($_POST['new_password']) != trim($_POST['new_password_again']))
			{
				return $this->_redirectMessage('两次输入密码不一致！', '请重新输入！', url('Manage::Base/ChangePass'), FAILURE);
			}
			
			$result = User::passwordChange($_POST['old_password'], $_POST['new_password']);
			if ($result['ack'] == SUCCESS)
			{
				$log = new VegeLog();
				$log->type = 1;
				$log->content = '修改密码';
				$log->user = CURRENT_USER_NAME;
				$log->ip = Helper_BSS_Normal::getIp();
				$log->time = date('Y-m-d H:i:s');
				$log->save();
				//QDB::getConn()->execute('update customer set c_pwd=\''.$_POST['new_password'].'\' where c_no=\''.CURRENT_USER_NAME.'\'');
				return $this->_redirectMessage('密码修改成功！', '更新用户密码成功。', url('Manage::Base/ChangePass'), $result['ack']);
			}
			else
			{
				return $this->_redirectMessage('密码错误！', $result['message'], url('Manage::Base/ChangePass'), $result['ack']);
			}
		}
		else
		{
			return $this->_redirect(url('Manage::Base/ChangePass'));
		}
	}
}